import { Global, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import * as redisStore from 'cache-manager-redis-yet';
import { RedisCacheService } from './redisCache.service';
import { CacheModule } from '@nestjs/cache-manager';
import { redisConfig } from 'src/config/redis.config';

@Global()
@Module({
  imports: [
    CacheModule.registerAsync({
        useFactory: async () => ({
            store: redisStore.redisStore,
            url: `redis://${redisConfig.host}:${redisConfig.port}`,
            port: redisConfig.port,
            password: redisConfig.password,
            database: Number(!process.env.ServerApp ? 2 : process.env.redisdbcache),
            isGlobal: true,
        }),
      }),
  ],
  providers: [RedisCacheService],
  exports: [RedisCacheService] // This is IMPORTANT,  you need to export RedisCacheService here so that other modules can use it
})
export class RedisCacheModule {}
